Skip to content

Conversation

@rickeylev
Copy link
Collaborator

@rickeylev rickeylev commented Sep 6, 2025

This changes the system_python bootstrap to use a 2-stage process like the script
bootstrap does. Among other things, this means the import paths are written to
a generated file (bazel_site_init.py, same as boostrap=script) and sys.path
setup is performed by the Python code in stage 2.

Since the PYTHONPATH environment variable isn't used, this fixes the problem on
Windows where the value is too long.

This also better unifies the system_python and script based bootstraps because the
same stage 2 code and bazel_site_init code is used.

Along the way, several other improvements:

  • Fixes path ordering for system_python. The order now matches venv ordering
    (stdlib, binary paths, runtime site packages).
  • Makes the venv-based solution work when the site module is disabled (-S).
  • Makes interpreter_args attribute and
    RULES_PYTHON_ADDITIONAL_INTERPRETER_ARGS env var work with system_python.
  • Makes main_module work with system_python.
  • Progress towards a supportable non-shell based bootstrap (a user requested
    this because their environment doesn't install any shells as a security
    precaution).

Fixes #2652

@rickeylev rickeylev requested a review from aignas as a code owner September 6, 2025 04:46
@rickeylev rickeylev marked this pull request as draft September 6, 2025 05:04
Copy link
Collaborator

@aignas aignas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gave a quick look at this and I think the change is really exciting. I'll do a thorough review once it is ready for review.

@rickeylev
Copy link
Collaborator Author

fyi

@mering -- non-shell based bootstrap (still needs a system python, so not our coveted native launcher, but doesn't require you to have a shell installed)

@jacky8hyf -- should make bootstrap=script work with site disabled (-S)

@rickeylev rickeylev marked this pull request as ready for review September 6, 2025 08:57
@rickeylev
Copy link
Collaborator Author

Ok, ready for review

Copy link
Collaborator

@aignas aignas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really cool, thank you!

@rickeylev rickeylev enabled auto-merge September 6, 2025 17:23
@rickeylev rickeylev added this pull request to the merge queue Sep 6, 2025
Merged via the queue into bazel-contrib:main with commit b8e32c4 Sep 6, 2025
3 of 4 checks passed
@rickeylev rickeylev deleted the refactor.two.stage.systempython branch September 6, 2025 17:38
github-merge-queue bot pushed a commit that referenced this pull request Jan 25, 2026
…y_binary (#3495)

### Problem
In #3242, it looks like `rules_python` introduced new template
placeholders in the bootstrap scripts: `%stage2_bootstrap%` and
`%interpreter_args%`. And from what I can tell, also made their
successful substitution a requirement.

This works totally fine when the caller is calling `rules_python`
directly. However, when external repositories (like gRPC's cython)
define py_binary using the native rule, these placeholders don't seem to
be substituted? The result is that the literal placeholder text ends up
in the generated bootstrap scripts, causing `SyntaxError`s or
file-not-found errors at runtime

### Fix

Detect if the `%stage2_bootstrap%` variable isn't expanded and fallback
to
`%main%` which IS substituted even for native `py_binary`.

For `%interpreter_args%`, wrap it in triple-quotes so it's hopefully
always valid Python syntax, then detect the sentinel and default to an
empty list. This is a bit hacky,
but is fairly non-invasive.

---------

Co-authored-by: Richard Levasseur <rlevasseur@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Switching to modules causes LoadLibrary to fail in some python extensions due to MAX_PATH length

2 participants